<?php

/*  
==================================================================
1. PROFESI
==================================================================
*/

function list_profesi(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=profesi_delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Profesi'=>array('style'=>'border-bottom:2px solid;width:50%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM profesi ";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=profesi&task=profesi_create&id='.$ey['profesi_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		
		$detailproperty = array(
			'href'=>'index.php?com=profesi&task=list_profesi_detail&pid='.$ey['profesi_id'],
			'title'=>'Detail Profesi'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT profesi_detail_id FROM profesi_detail 
			WHERE profesi_id=".$ey['profesi_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['profesi_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_profesi'] ,   
			'jabatan'=>  $ey['jabatan'] ,   
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row);   
	return table_builder($header , $datas ,  3 , false );
} 

function form_profesi_create($id = 0){
	$view = form_header( "profesi" , "profesi"  );
 	$fields = my_get_data_by_id('profesi','profesi_id',$id);
	 
	$nama = array(
			'name'=>'nama_profesi',
			'value'=>( isset($_POST['nama_profesi']) ? $_POST['nama_profesi'] : $fields['nama_profesi'] ),
			'id'=>'nama_profesi',
			'type'=>'text',
			'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Profesi");

	$atemps=array();
	$atemps["Dokter"]="Dokter" ;
	$atemps["Perawat"]="Perawat" ;
	$temp = array(
		'name'=>'jabatan',
		'value'=>($fields ? $fields['jabatan'] : "" ),
		'id'=>'jabatan' 
	);
	$form_temp = form_dropdown($temp , $atemps );
	$view .= form_field_display( $form_temp   , "Jabatan");
		
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_profesi_submit($id=0){
	$datas = array(
		'nama_profesi'=>my_type_data_str( $_POST['nama_profesi'] ),
		'jabatan'=>my_type_data_str( $_POST['jabatan'] ),
	);
	if($id > 0){
		return my_update_record('profesi' ,'profesi_id' , $id , $datas);
	}
	
	return my_insert_record('profesi' , $datas);
} 

function form_profesi_validate($profesi_id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_profesi"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Profesi belum diisi";
	}

	$query="SELECT profesi_id FROM profesi 
		WHERE nama_profesi='".$nama.
		"' AND profesi_id<>".$profesi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah ada";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_profesi($profesi_id){
	$query = "DELETE FROM profesi WHERE profesi_id ={$profesi_id} ";
	return my_query($query);
}	

/*  
==================================================================
2. Detail Profesi
==================================================================
*/

function list_profesi_detail($pid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=profesi_detail_delete&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Detail Profesi'=>array('style'=>'border-bottom:2px solid;width:85%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$query = "SELECT * FROM profesi_detail WHERE profesi_id ={$pid}";
 	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=profesi&task=profesi_detail_create&pid='.$pid.'&id='.$ey['profesi_detail_id'],
			'rel'=>'facebox',
			'title'=>'profesi'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:;',
			'title'=>'Delete',
			'onclick'=>'confirmDelete('.$ey['profesi_detail_id'].')',
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'centre'), 
			'nama'=>  $ey['nama_profesi_detail'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row); 
	$profesi = my_get_data_by_id('profesi','profesi_id',$pid); 	 
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Profesi" , "Profesi"  );
	$view .= form_field_display( '<span class="label_form">'.ucfirst($profesi['nama_profesi']).'</span>', "<b>Profesi</b>"    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_profesi_detail_create($pid, $id = 0){
	$view = form_header( "profesi_sub" , "profesi_sub"  );
 	$fields = my_get_data_by_id('profesi_detail','profesi_detail_id',$id);
 	$profesi= my_get_data_by_id('profesi','profesi_id',$pid); 
	$nama = array(
		'name'=>'nama_profesi_detail',
		'value'=>( isset($_POST['nama_profesi_detail']) ? $_POST['nama_profesi_detail'] : $fields['nama_profesi_detail'] ),
		'id'=>'nama_profesi_detail',
		'type'=>'text',
		'size'=>'45'
	);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Detail Profesi");
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_profesi_detail_submit($pid , $id =0){
	$datas = array(
		'profesi_id'=>my_type_data_int($pid),	
		'nama_profesi_detail'=>my_type_data_str( $_POST['nama_profesi_detail'] ),
	);
	
	if($id > 0){
		return my_update_record('profesi_detail' ,'profesi_detail_id' , $id , $datas);
	}
	return my_insert_record('profesi_detail' , $datas);
} 

function form_profesi_detail_validate($pid,$id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_profesi_detail"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Detail profesi belum diisi";
	}

	$query="SELECT profesi_detail_id FROM profesi_detail 
		WHERE nama_profesi_detail='".$nama.
		"' AND profesi_id=".$pid; 
		"' AND profesi_detail_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Detail profesi sudah ada";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_profesi_detail($id){
	$query = "DELETE FROM profesi_detail WHERE profesi_detail_id ={$id} ";
	return my_query($query);
}	
